package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zzq.dao.OrdersettingMapper;
import com.zzq.pojo.Ordersetting;
import com.zzq.pojo.OrdersettingExample;
import com.zzq.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 幼儿猿大班
 * 2021/7/26
 */
@Service(interfaceClass = OrdersettingService.class)
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    @Transactional
    public void insertBatch(ArrayList<Ordersetting> ordersettings) {
        for (Ordersetting ordersetting : ordersettings) {
            OrdersettingExample ordersettingExample=new OrdersettingExample();
            ordersettingExample.createCriteria().andOrderdateEqualTo(ordersetting.getOrderdate());
            long l = ordersettingMapper.countByExample(ordersettingExample);
            if (l>0) {
                ordersettingMapper.updateByExampleSelective(ordersetting,ordersettingExample);
            }else{
                ordersettingMapper.insertSelective(ordersetting);
            }
        }
    }

    @Override
    @Transactional
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {
        OrdersettingExample ordersettingExample = new OrdersettingExample();

        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(date+"-1");
            endDate = new SimpleDateFormat("yyyy-MM-dd").parse(date+"-31");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ordersettingExample.createCriteria().andOrderdateBetween(beginDate,endDate);
        List<Ordersetting> ordersettings = ordersettingMapper.selectByExample(ordersettingExample);
        ArrayList<Map<String, Integer>> maps = new ArrayList<>();
        for (Ordersetting ordersetting : ordersettings) {
            Map<String, Integer> stringIntegerMap = new HashMap<>();
            stringIntegerMap.put("date",ordersetting.getOrderdate().getDate());
            stringIntegerMap.put("number",ordersetting.getNumber());
            stringIntegerMap.put("reservations",ordersetting.getReservations());
            for (String s : stringIntegerMap.keySet()) {
                System.out.print(s+" ");
            }
            maps.add(stringIntegerMap);
        }
        return maps;
    }

    @Override
    @Transactional
    public void setNumByDate(Ordersetting ordersetting) {
        OrdersettingExample ordersettingExample=new OrdersettingExample();
        ordersettingExample.createCriteria().andOrderdateEqualTo(ordersetting.getOrderdate());
        long l = ordersettingMapper.countByExample(ordersettingExample);
        if (l>0) {
            ordersettingMapper.updateByExampleSelective(ordersetting,ordersettingExample);
        }else{
            ordersettingMapper.insertSelective(ordersetting);
        }
    }
}
